﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models;
using JNPF.Basics.Models.System.Dtos.BillRule;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 单据规则
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-04-20
    /// </summary>
    [Route("api/System/[controller]")]
    public class BillRuleController : BaseApiController
    {
        private BillRuleBll billRuleBll = new BillRuleBll();

        #region GET
        /// <summary>
        /// 获取单据规则列表(带分页)
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public async Task<ResponseResult> GetList([FromQuery] PageInput input)
        {
            var data = await billRuleBll.GetList(input);
            var output = new PageResult<BillRuleListOutput>
            {
                list = data.list.MapToList<BillRuleListOutput>(),
                pagination = data.pagination
            };
            return Success(output);
        }

        /// <summary>
        /// 信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("Selector")]
        public async Task<ResponseResult> GetSelector()
        {
            var data = (await billRuleBll.GetList()).MapToList<BillRuleListOutput>().FindAll(x => x.enabledMark == 1);
            return Success(new { list = data });
        }

        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ResponseResult> GetInfo(string id)
        {
            var data = (await billRuleBll.GetInfo(id)).MapTo<BillRuleInfoOutput>();
            return Success(data);
        }

        /// <summary>
        /// 获取单据流水号（工作流程调用）
        /// </summary>
        /// <param name="enCode">编码</param>
        /// <returns></returns>
        [HttpGet("BillNumber/{enCode}")]
        public async Task<ResponseResult> GetBillNumber(string enCode)
        {
            var data = await billRuleBll.GetBillNumber(enCode, true);
            return Success("获取成功", data);
        }
        #endregion

        #region POST
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(string id)
        {
            var entity = await billRuleBll.GetInfo(id);
            if (entity != null)
            {
                if (!string.IsNullOrEmpty(entity.OutputNumber))
                {
                    return Error("单据已经被使用,不允许被删除");
                }
                else
                {
                    await billRuleBll.Delete(entity);
                }
            }
            return Success("删除成功");
        }

        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Create([FromBody] BillRuleCrInput input)
        {
            if (await billRuleBll.IsExistByEnCode(input.enCode, null))
            {
                return Error("编码已存在");
            }
            if (await billRuleBll.IsExistByFullName(input.fullName, null))
            {
                return Error("名称已存在");
            }
            var entity = input.MapTo<BillRuleEntity>();
            var isOk = await billRuleBll.Create(entity);
            return isOk > 0 ? Success("新建成功") : Error("新建失败");
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(string id, [FromBody] BillRuleUpInput input)
        {
            if (await billRuleBll.IsExistByEnCode(input.enCode, id))
            {
                return Error("编码已存在");
            }
            if (await billRuleBll.IsExistByFullName(input.fullName, id))
            {
                return Error("名称已存在");
            }
            var entity = input.MapTo<BillRuleEntity>();
            var isOk = await billRuleBll.Update(id, entity);
            return isOk > 0 ? Success("更新成功") : Error("更新失败");
        }

        /// <summary>
        /// 更新单据规则状态
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpPut("{id}/Actions/State")]
        public async Task<ResponseResult> ActionsState(string id)
        {
            var entity = await billRuleBll.GetInfo(id);
            entity.EnabledMark = entity.EnabledMark == 0 ? 1 : 0;
            var isOk = await billRuleBll.Update(id, entity);
            return isOk > 0 ? Success("更新状态成功") : Error("更新状态失败");
        }
        #endregion
    }
}